var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
	conn.createChannel(function(err, ch) {
		var q = 'rpc_queue'; // linsten on 'rpc_queue'
		ch.assertQueue(q, {
			durable: false
		});
		ch.prefetch(1);
		console.log(' [x] awaiting RPC requests');

		ch.consume(q, function(msg) {
			// get a rpc request
			console.log(' [x] receive %s, corrId=%s, replyTo=%s', 
				msg.content, msg.properties.correlationId, msg.properties.replyTo);
			var content = msg.content.toString();
			var r = doSth(content);
			// do sth, but here wo simple echo 
			ch.sendToQueue(msg.properties.replyTo,
				new Buffer(r), {
					correlationId: msg.properties.correlationId
				});

			// ack
			ch.ack(msg);
		});
	});
});


function doSth(content) {
	return content;
}